package com.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.hronline.R;
import com.models.Item;
import com.models.ManagerSummary;
import com.models.Notif;
import com.models.SectionItem;

public class ManagerDashboardAdapter extends ArrayAdapter<Item> {

	private Context context;
	private LayoutInflater inflater;
	public ArrayList<Item> arrItem;
	
	public ManagerDashboardAdapter(Context context, ArrayList<Item> arrItem) {
		super(context,0, arrItem);
		// TODO Auto-generated constructor stub
		
		this.context	=	context;
		this.arrItem	=	arrItem;
		this.inflater	=	(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		final Item i = this.arrItem.get(position);
		if (i != null) {
			
			if(i.isSection()){
				SectionItem si = (SectionItem)i;
				v = this.inflater.inflate(R.layout.section_header, null);
				
				TextView sectionView = (TextView) v.findViewById(R.id.txtHeaderTitle);
				sectionView.setText(si.getTitle());
				
			}
			else{
				/* check the item is the manager summary or the notification */
				if(i instanceof ManagerSummary){ // this is manager summary section
					v	=	this.inflater.inflate(R.layout.manager_summary_dashboard, null);
					v.setPadding(0, 0, 0, 10);
					
					ManagerSummary managerSummary	=	(ManagerSummary)i;
					
					TextView txtAbsenceRequest	=	(TextView)v.findViewById(R.id.txtAbsenceRequest);
					TextView txtAbsentToday		=	(TextView)v.findViewById(R.id.txtAbsentToday);
					TextView txtOpenSickness	=	(TextView)v.findViewById(R.id.txtOpenSickness);
					
					/* display text */
					txtAbsenceRequest.setText(managerSummary.getAbsenceRequest());
					txtAbsentToday.setText(managerSummary.getAbsentToday());
					txtOpenSickness.setText(managerSummary.getOpenSickness());
					
					/* handle click on the button */
					View absenceRequestView	=	v.findViewById(R.id.btnAbsenceRequest);
					View myTaskView			=	v.findViewById(R.id.btnMyTask);
					View absentTodayView	=	v.findViewById(R.id.btnAbsentToday);
					View openSicknessView	=	v.findViewById(R.id.btnOpenSickness);
					
					absenceRequestView.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							openAbsenceRequestPage();
						}
					});
					
					myTaskView.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							openMyTaskPage();
						}
					});
					
					absentTodayView.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							openAbsentTodayPage();
						}
					});
					
					openSicknessView.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							openSicknessPage();
						}
					});
					
				}
				else{// this is today notification list
					
					Notif todayNotification	=	(Notif)i;
					
					v	=	this.inflater.inflate(R.layout.text_row, null);
					TextView txtContent	=	(TextView)v.findViewById(R.id.txtContent);
					txtContent.setText(todayNotification.getDescription());
				}
			}
		}
		return v;
	}
	
	/* this method is used to open absence request page */
	private void openAbsenceRequestPage(){
		Log.i("duylept", "open absence request");
	}
	
	/* this method is used to open my task page */
	private void openMyTaskPage(){
		Log.i("duylept", "open my task page");
	}
	
	/* this method is used to open absent today page */
	private void openAbsentTodayPage(){
		Log.i("duylept", "open absent today page");
	}
	
	/* this method is used to open sickness page */
	private void openSicknessPage(){
		Log.i("duylept", "open sickness page");
	}

}
